package service;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.DateKits;
import common.kit.PageSql;
import model.LiquidRecycle;
import model.Staff;
import model.Station;

public class LiquidRecycleService extends BaseService {

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tlr.id,tlr.createTime,tlr.wasteName,tlr.count,tlr.way,tlr.fate,ts.name,tsr.name AS servicerName,tc.name AS companyName1,tlr.recorder " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_liquid_recycle tlr ") ;
	    pageSql.join(" t_station ts " , " ts.id = tlr.stationId ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = tlr.serviserId ") ;
	    pageSql.join(" t_company tc " , " tc.id = ts.companyId ") ;
		pageSql.setNotLessThanCondition("tlr.createTime","tlr.createTime.start") ; //创建时间
		pageSql.setNotMoreThanCondition("tlr.createTime","tlr.createTime.end") ; //创建时间
		pageSql.setEqualCondition("tlr.stationId") ; //点位	 
		pageSql.setLikeCondition("ts.name") ; //点位名称	 
		pageSql.setLikeCondition("tsr.name") ; //运维公司	 
		pageSql.setLikeCondition("tc.name") ; //企业名称	 
	    pageSql.orderBy(" tlr.createTime DESC ");
		return pageSql.paginate( pageNumber , pageSize) ;
	}

	public Page<Record> getApiPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tlr.id,tlr.createTime,tlr.recorder,tlr.wasteName,tlr.count,tlr.way,tlr.fate,ts.name " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_liquid_recycle tlr ") ;
	    pageSql.join(" t_servicer ts " , " ts.id = tlr.serviserId ") ;
		pageSql.setEqualCondition("tlr.stationId") ; //点位	 
		pageSql.setNotLessThanCondition("tlr.createTime", "startTime");
		pageSql.setNotMoreThanCondition("tlr.createTime", "endTime");
	    pageSql.orderBy(" tlr.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		List<Record> list = page.getList();
		for (Record record : list) {
			record.set("createTime", DateKits.toStr(record.getDate("createTime") , DateKits.MINITE));		
		}
		return page ;
	}

	public Ret save(LiquidRecycle liquidRecycle, Station station , Staff staff) {
		liquidRecycle.setServiserId(staff.getServicerId());
		liquidRecycle.setRecorder(staff.getName());
		liquidRecycle.setStationId(station.getId());
		liquidRecycle.save();
	    return ok() ;
	}
}
